#include  "lp_global_vars.h"

void compute_performance(int sd, short event, void* arg) {
	UNUSED_ARG(sd);
	UNUSED_ARG(event);
	UNUSED_ARG(arg);
	//	if (lp_interval_performance->highest_deliverable_inst_id > -1)
	{
		gettimeofday(&performance_end_time, NULL);
		gettimeofday(&endTime_Total, NULL);

		lp_interval_performance->interval_time = measure_time_difference(performance_end_time, performance_start_time);
		lp_interval_performance->total_time = measure_time_difference(endTime_Total, startTime_Total);
		lp_interval_performance->interval_delivery_throughput = calculate_throughput((sizeof(proposer_msg) - PROPOSER_MSG_HEADER_SIZE) * lp_interval_performance->interval_num_packets_delivered, lp_interval_performance->interval_time);
		lp_interval_performance->interval_sending_throughput = calculate_throughput(lp_interval_performance->interval_bytes_sent, lp_interval_performance->interval_time);
		lp_interval_performance->interval_recv_throughput = calculate_throughput(lp_interval_performance->interval_bytes_recvied, lp_interval_performance->interval_time);
		lp_interval_performance->interval_latency = ((lp_interval_performance->interval_latency) / (double) lp_interval_performance->interval_num_packets_delivered);
		lp_store_performance_in_array(&lp_performances[0], &lp_next_performance_index, lp_interval_performance);

		lp_print_performance(lp_interval_performance);
		lp_nulify_performance(lp_interval_performance);
		if (lp_next_performance_index >= STOPPING_CONDITION) {
			lp_store_performance_in_file(lp_performance_file, lp_latency_file, lp_next_performance_index);
			free(data);
			exit(1);
		}
	}
	gettimeofday(&performance_start_time, NULL);
	int status = evtimer_add(print_stat_timeout_eve, &tv1);
	assert(status >= 0);
}
double measure_time_difference(struct timeval now, struct timeval past) {
	struct timeval elapsed_interval;

	elapsed_interval.tv_sec = now.tv_sec - past.tv_sec;
	elapsed_interval.tv_usec = (now.tv_usec > past.tv_usec ? now.tv_usec - past.tv_usec : past.tv_usec - now.tv_usec);
	double msec_interval = 1000 * elapsed_interval.tv_sec + elapsed_interval.tv_usec / (double) 1000;

	return msec_interval;
}

double calculate_throughput(int bytes, double elapsed_time) {
	double thr = (bytes / (double) (1024 * 1024));
	thr = thr / (double) elapsed_time;
	thr = thr * 1000 * 8;
	return thr;
}

void lp_print_performance(lp_performance *p) {
	printf("------------------------------------------------------------------------------------------------------------------\n");
	printf("TOTAL_TIME:   %f INTERVAL_TIME: %f  TICKS_PER_SECOND: %f\n", p->total_time, p->interval_time, p->interval_num_entrance);
	printf("INTERVAL DELI: 	   NUM_PACKETS: %d, THROUGHPUT: %f\n", p->interval_num_packets_delivered, p->interval_delivery_throughput);
	printf("INTERVAL SENT: 	   BYTES: %f, THROUGHPUT: %f\n", p->interval_bytes_sent, p->interval_sending_throughput);
	printf("INTERVAL RECV: 	   BYTES: %f, THROUGHPUT: %f\n", p->interval_bytes_recvied, p->interval_recv_throughput);
	printf("INTERVAL HOLES:       %d, \n", p->interval_num_holes);
	printf("INTERVAL REDUNDANT PACKETS:     %d  LOST_PACKETS: %d\n", p->interval_num_redundant_packets_received, p->interval_num_packets_lost);
	printf("TOTAL    REDUNDANT PACKETS:     %d\n\n", p->total_num_redundant_packets_received);
	printf("LATENCY: %f\n\n", p->interval_latency);
	printf("repeated request: %d [local HD: %d   global HD: %d] [local gapEnd: %d	next_prop: %d]\n", p->interval_repeated_requets, p->last_delivered, p->highest_deliverable_inst_id, p->last_gapless_delivered, p->next_to_propose_slot_number);

}
void lp_nulify_performance(lp_performance *p) {
	p->interval_time = 0;
	p->interval_num_packets_delivered = 0;
	p->interval_delivery_throughput = 0;
	p->interval_num_packets_sent = 0;
	p->interval_sending_throughput = 0;
	p->interval_num_holes = 0;
	p->interval_num_redundant_packets_received = 0;
	p->interval_num_packets_lost = 0;
	p->interval_num_entrance = 0;
	p->interval_latency = 0;
	p->interval_num_packets_received = 0;
	p->interval_num_packets_lost = 0;
	p->interval_num_overwritten_insts = 0;
	p->interval_bytes_recvied = 0;
	p->interval_bytes_sent = 0;
	p->interval_recv_throughput = 0;
	p->interval_repeated_requets = 0;

	if (lp_next_performance_index == 0) {
		p->latency_counter = 0;
		p->next_to_propose_slot_number = 0;
		p->last_delivered = -1;
		p->last_gapless_delivered = -1;
	}
}
void lp_store_performance_in_file(const char * performance_file, const char * lat_file, int num_entries) {

	FILE *fp;
	int i;
	fp = fopen(performance_file, "a");
	assert(fp != NULL);
	for (i = DISCARD; i < num_entries - DISCARD; i++) {
		fprintf(fp, "%d %d %f %d %f %f %f %f %f %d %d %f %d %f %d %d\n", i + 1, SUBMIT_PER_WAKE, lp_performances[i].interval_time, lp_performances[i].interval_num_packets_delivered, lp_performances[i].interval_delivery_throughput, lp_performances[i].interval_bytes_sent, lp_performances[i].interval_sending_throughput, lp_performances[i].interval_bytes_recvied, lp_performances[i].interval_recv_throughput, lp_performances[i].interval_num_holes, lp_performances[i].interval_num_redundant_packets_received, lp_performances[i].interval_num_entrance, lp_performances[i].highest_deliverable_inst_id, lp_performances[i].interval_latency, lp_performances[i].interval_num_packets_lost, lp_performances[i].interval_repeated_requets);
	}
	fclose(fp);
	fp = fopen(lat_file, "w");
	assert(fp != NULL);

	for (i = 0; i < lp_interval_performance->latency_counter; i++)
		fprintf(fp, "%f\n", latency[i]);

	fclose(fp);

}
void lp_store_performance_in_array(lp_performance * performances_array, int * performance_index, lp_performance *i_p) {

	memcpy((performances_array + *performance_index), i_p, sizeof(lp_performance));
	*(performance_index) = *(performance_index) + 1;
}
